package com.xc.erp.modules.erp.service.impl;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.support.ExcelTypeEnum;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.xc.erp.common.annotation.Log;
import com.xc.erp.common.listener.JshSupplierListener;
import com.xc.erp.modules.erp.domain.dto.SupplierNameParam;
import com.xc.erp.modules.erp.domain.dto.SupplierParam;
import com.xc.erp.modules.erp.domain.pojo.JshSupplier;
import com.xc.erp.modules.erp.domain.vo.SupplierVo;
import com.xc.erp.modules.erp.mapper.JshSupplierMapper;
import com.xc.erp.modules.erp.service.JshSupplierService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.List;

/**
 * 用户、会员、供应商
 */
@Service
public class JshSupplierServiceImpl extends ServiceImpl<JshSupplierMapper, JshSupplier>
    implements JshSupplierService {

    @Override
    public IPage<JshSupplier> selectSupplierPage(SupplierParam supplierParam, Integer page, Integer limit) {
        IPage<JshSupplier> pageModel = new Page<>(page,limit);

        QueryWrapper<JshSupplier> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(supplierParam.getSupplier())){
            wrapper.like("supplier",supplierParam.getSupplier());
        }
        if(!StringUtils.isEmpty(supplierParam.getType())){
            wrapper.eq("type",supplierParam.getType());
        }
        if(!StringUtils.isEmpty(supplierParam.getTelephone())){
            wrapper.like("telephone",supplierParam.getTelephone());
        }
        if(!StringUtils.isEmpty(supplierParam.getPhonenum())){
            wrapper.like("phone_num",supplierParam.getPhonenum());
        }
        wrapper.orderByAsc("sort");
        pageModel = baseMapper.selectPage(pageModel,wrapper);

        return pageModel;
    }

    @Override
    public boolean checkIsNameAndTypeExist(SupplierNameParam supplierNameParam) {
        QueryWrapper<JshSupplier> wrapper = new QueryWrapper<>();
        if(!StringUtils.isEmpty(supplierNameParam.getName())){
            wrapper.eq("supplier",supplierNameParam.getName());
        }
        if(!StringUtils.isEmpty(supplierNameParam.getType())){
            wrapper.eq("type",supplierNameParam.getType());
        }
        if(!StringUtils.isEmpty(supplierNameParam.getId())){
            wrapper.eq("id",supplierNameParam.getId());
        }
        JshSupplier supplier = baseMapper.selectOne(wrapper);
        if(null == supplier){
            return false;
        }
        return true;
    }

    @Override
    public void exportExcel(SupplierParam supplierParam, HttpServletResponse response) {
        try {
            //设置下载信息
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            String fileName = URLEncoder.encode(supplierParam.getType(),"UTF-8");
            //下载操作
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + ExcelTypeEnum.XLSX.getValue());

            //查询数据库
            QueryWrapper<JshSupplier> wrapper = new QueryWrapper<JshSupplier>();
            if(!StringUtils.isEmpty(supplierParam.getSupplier().trim())){
                wrapper.like("supplier",supplierParam.getSupplier());
            }
            if(!StringUtils.isEmpty(supplierParam.getType().trim())){
                wrapper.eq("type",supplierParam.getType());
            }
            if(!StringUtils.isEmpty(supplierParam.getTelephone().trim())){
                wrapper.like("telephone",supplierParam.getTelephone());
            }
            if(!StringUtils.isEmpty(supplierParam.getPhonenum().trim())){
                wrapper.like("phone_num",supplierParam.getPhonenum());
            }
            //查询的数据集合
            List<JshSupplier> supplierList = baseMapper.selectList(wrapper);

            List<SupplierVo> jshSupplierList = new ArrayList<SupplierVo>(supplierList.size());

            for (JshSupplier jshSupplier : supplierList) {
                SupplierVo supplierVo = new SupplierVo();
                BeanUtils.copyProperties(jshSupplier,supplierVo);
                supplierVo.setEnabled(jshSupplier.getEnabled() == true  ? "启用":"禁用");
                jshSupplierList.add(supplierVo);
            }

            EasyExcel.write(response.getOutputStream(),SupplierVo.class)
                    .sheet(supplierParam.getType() + "信息")
                    .doWrite(jshSupplierList);

        } catch (Exception e) {
            e.printStackTrace();
        }

    }

    @Override
    public void importExcel(MultipartFile file) {
        try {
            EasyExcel
                    .read(file.getInputStream(),SupplierVo.class,new JshSupplierListener(baseMapper))
                    .sheet()
                    .doRead();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Transactional(rollbackFor = RuntimeException.class)
    @Override
    public void batchSetStatus(boolean status, Long[] ids) {
        List<JshSupplier> supplierList = new ArrayList<>();
        for (int i = 0; i < ids.length; i++) {
            JshSupplier supplier = new JshSupplier();
            supplier.setEnabled(status);
            supplier.setId(ids[i]);
            supplierList.add(supplier);
        }
        this.updateBatchById(supplierList);
    }

    @Override
    public List<JshSupplier> supplierList(String type) {
        return list(new QueryWrapper<JshSupplier>().eq("type",type));
    }

}




